# encoding: utf-8 
'''
Created on 2018-6-13
description: 配置读取助手类
@author: zhujianhua
'''
import re
import traceback  

class ConfigReader:
    #默认配置文件路径
    filePath = './etc/mon.cfg'
    
    
    def __init__(self,filePath):
        #配置字典
        self.configDict = {}
        self.key = ''
        self.filePath = filePath
        self._read()
        return
    
    
    def _read(self):
        try:
            reader = open(self.filePath)
            lines = reader.readlines()
            
            for line in lines:
                line = line.strip()#除去前后空字符串
                if re.compile('\#').match(line) or '' == line: #跳过以#开头的注释，[]中括号的内容和空行
                    continue
                elif re.compile('^\\[\\w*\\]$').match(line):
                    self.configDict[line[1:-1]] = {}
                    self.key = line[1:-1]
                else:   
                    splitStr = line.split('=')
                    self.configDict[self.key][splitStr[0]] = splitStr[1]
        except:
            traceback.print_exc()
        finally:
            reader.close()
            
            
            
    #获取配置信息        
    def get(self,*key):
        return self.configDict[key[0]][key[1]]
